/*
 * @Author: xuesong
 * @Date: 2022-01-11 14:34:19
 * @LastEditors: xuesong
 * @LastEditTime: 2022-01-18 20:54:15
 * @FilePath: \BLE-SDK-SVN499-V05\projects\ble_peripheral\uart_ble\src\Remote\user_Bz.c
 */
#include "OSAL.h"
#include "ll.h"
#include "log.h"
#include "pwm.h"
#include "clock.h"
#include "peripheral.h"
#include "userApp.h"
#include "user_prcess.h"
#include "simpleBLEPeripheral.h"
#include "user_Bz.h"
//-----------------------------------------------
User_BZ_t   UseBzData;

#define	  PWM_TOP_VALUE     256
#define	  PWM_COM_VALUE	    128

PwmCfg_t  BZpwm_config;
uint32    Bz_ON_Time = 0;
//-----------------------------------------------
static  void  BZ_ON(void);
static  void  BZ_OFF(void);
//-----------------------------------------------

void Bz_IO_Init(void)
{
    HalGpioSet(BZ_PIN,Bit_DISABLE);
    HalGpioPinInit(BZ_PIN, GPIO_OUTPUT);
    HalGpioPupdConfig(BZ_PIN,GPIO_PULL_UP_S);
	BZpwm_config.pwmN = PWM_CH0;
	BZpwm_config.pwmMode = PWM_CNT_UP;
	BZpwm_config.pwmPin = BZ_PIN;
	BZpwm_config.pwmDiv = PWM_CLK_DIV_16;
	BZpwm_config.pwmPolarity = PWM_POLARITY_RISING;
	BZpwm_config.cmpVal	= PWM_COM_VALUE;
	BZpwm_config.cntTopVal = PWM_TOP_VALUE;
	HalPwmModuleInit();
	// HalPwmChannelStart(BZpwm_config);
}

void SwithBZPWM(uint8 value)
{
    BZpwm_config.cmpVal	= value;
}

void BZ_Precss(void)
{
    if(UseBzData.flag & B_BZ_RUN)
    {
        if(UseBzData.flag & B_BZ_ON)
        {
            BZ_OFF();
            LOG("Bz atuo 0 off\r\n");
            UseBzData.flag &= ~B_BZ_ON;
            UseBzData.Count --;
            if(UseBzData.Count > 0)
                useApp_task_set(USER_TASK_START,UserAppEvent_Bz,UseBzData.OffTime);  
            else{
                UseBzData.flag &= ~B_BZ_RUN;
            }            
        }else{
            BZ_ON();
            Bz_ON_Time = rtc_get_counter();
            LOG("Bz atuo 0 on:");
            UseBzData.flag |= B_BZ_ON;
            useApp_task_set(USER_TASK_START,UserAppEvent_Bz,UseBzData.OnTime);  
        }

    }else{
        BZ_OFF();
        LOG("Bz atuo 1 off\r\n");
    }
}


void Bz_init_ONOFF(User_BZ_t bzData)
{
    // HalGpioSet(BZ_PIN,Bit_ENABLE);	
    LOG("BZ flag = 0x%X\n",UseBzData.flag);
    if(UseBzData.flag & B_BZ_Disable)
    {
        BZ_OFF();
        UseBzData.flag &= ~(B_BZ_RUN|B_BZ_KeyOn);
        LOG("Bz disable\r\n");
    }else{
        if((bzData.flag & B_BZ_RUN))
        {
            UseBzData.flag &= (B_BZ_Disable | B_BZ_KeyOn);
            UseBzData.flag |= B_BZ_RUN;
            UseBzData.Count = bzData.Count;
            UseBzData.OffTime = bzData.OffTime;
            UseBzData.OnTime = bzData.OnTime;
            if(UseBzData.OnTime == 0)           //0 as defualt 100ms
                UseBzData.OnTime = 100; 
            if(UseBzData.OffTime == 0)          //0 as defualt 200ms
                UseBzData.OffTime = 200;
            useApp_task_set(USER_TASK_START,UserAppEvent_Bz,NULL);  
            LOG("Bz on %d times\r\n",UseBzData.Count);
        }else{
            // HalPwmChannelStop(BZpwm_config);
            BZ_OFF();
            UseBzData.flag &= ~(B_BZ_RUN|B_BZ_KeyOn);
            LOG("Bz off\r\n");
        }
    }
}

static  void  BZ_OFF(void)
{
	HalPwmChannelStop(BZpwm_config.pwmN);
    HalGpioSet(BZ_PIN,Bit_DISABLE);
    HalGpioPinInit(BZ_PIN, GPIO_OUTPUT);
    HalGpioPupdConfig(BZ_PIN,GPIO_FLOATING);
}

static  void  BZ_ON(void)
{
    // HalGpioSet(BZ_PIN,Bit_ENABLE);
    // HalGpioPinInit(BZ_PIN, GPIO_OUTPUT);
    // HalGpioPupdConfig(BZ_PIN,GPIO_PULL_UP_S);  
	HalPwmChannelStart(BZpwm_config);
}



